const enviromentService = require('../service/enviromentService');

module.exports = {
    fetchEnvList: async (ctx) => {
        const list = await enviromentService.findAllEnv();
        ctx.body = list;
    },
    saveEnv: async (ctx) => {
        const env = await enviromentService.saveEnv(ctx.reqParams.body);
        ctx.body = env;
        ctx.state.message = "保存成功";
    },
    mergeEnv: async (ctx) => {
        const logs = await enviromentService.merge(ctx.reqParams.body);
        ctx.body = logs;
        if(logs.some((log) => log.status === 'FAIL')) {
            ctx.state.message = "存在项目合并失败，请查看";
        } else {
            ctx.state.message = "合并成功";
        }
    },
    //mergeEnv失败的项目 通过这个接口合并
    mergeProject: async (ctx) => {
        const logs = await enviromentService.mergeProject(ctx.reqParams.body);
        ctx.body = logs;
        if(logs.some((log) => log.status === 'FAIL')) {
            ctx.state.message = "合并失败";
        } else {
            ctx.state.message = "合并成功";
        }
    },
    updateProtectBranch: async (ctx) => {
        const result = enviromentService.setProtectBranch(ctx.request.body);
        ctx.body = result;
        ctx.state.message = "设置成功";
    },
    updatePullRequest: async (ctx) => {
        const data = await enviromentService.updatePullRequest(ctx.reqParams.body);
        ctx.body = data;
    },
}